<template>
  <div class="workflow-definition">
    <!-- 搜索栏 -->
    <el-card class="search-card">
      <el-form :model="searchForm" ref="searchFormRef" inline>
        <el-form-item label="流程名称" prop="name">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入流程名称"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="流程类型" prop="type">
          <el-select
            v-model="searchForm.type"
            placeholder="请选择流程类型"
            clearable
            style="width: 120px"
          >
            <el-option label="贷款审批" value="loan" />
            <el-option label="风险审核" value="risk" />
            <el-option label="合同审批" value="contract" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="searchForm.status"
            placeholder="请选择状态"
            clearable
            style="width: 120px"
          >
            <el-option label="已发布" value="published" />
            <el-option label="未发布" value="unpublished" />
            <el-option label="已停用" value="disabled" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
          <el-button type="success" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        border
        stripe
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="name" label="流程名称" min-width="150" />
        <el-table-column prop="key" label="流程标识" width="120" />
        <el-table-column prop="type" label="流程类型" width="100">
          <template #default="scope">
            <el-tag :type="getTypeTag(scope.row.type)">
              {{ getTypeText(scope.row.type) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="version" label="版本" width="80" align="center" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusTag(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="160" />
        <el-table-column prop="updateTime" label="更新时间" width="160" />
        <el-table-column label="操作" width="280" fixed="right">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="handleDesign(scope.row)"
              v-if="scope.row.status !== 'published'"
            >设计</el-button>
            <el-button
              link
              type="primary"
              @click="handlePublish(scope.row)"
              v-if="scope.row.status === 'unpublished'"
            >发布</el-button>
            <el-button
              link
              type="primary"
              @click="handleDisable(scope.row)"
              v-if="scope.row.status === 'published'"
            >停用</el-button>
            <el-button
              link
              type="primary"
              @click="handleEnable(scope.row)"
              v-if="scope.row.status === 'disabled'"
            >启用</el-button>
            <el-button
              link
              type="primary"
              @click="handleView(scope.row)"
            >查看</el-button>
            <el-button
              link
              type="primary"
              @click="handleEdit(scope.row)"
              v-if="scope.row.status !== 'published'"
            >编辑</el-button>
            <el-button
              link
              type="danger"
              @click="handleDelete(scope.row)"
              v-if="scope.row.status !== 'published'"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
      append-to-body
    >
      <el-form
        :model="formData"
        :rules="formRules"
        ref="formRef"
        label-width="100px"
      >
        <el-form-item label="流程名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入流程名称" />
        </el-form-item>
        <el-form-item label="流程标识" prop="key">
          <el-input v-model="formData.key" placeholder="请输入流程标识" />
        </el-form-item>
        <el-form-item label="流程类型" prop="type">
          <el-select v-model="formData.type" placeholder="请选择流程类型">
            <el-option label="贷款审批" value="loan" />
            <el-option label="风险审核" value="risk" />
            <el-option label="合同审批" value="contract" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="formData.description"
            type="textarea"
            placeholder="请输入描述"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  name: '',
  type: '',
  status: ''
})

// 表格数据
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)
const tableData = ref([
  {
    name: '贷款审批流程',
    key: 'loan_approval',
    type: 'loan',
    version: 1,
    status: 'published',
    createTime: '2024-03-01 10:00:00',
    updateTime: '2024-03-01 10:00:00'
  }
])

// 表单数据
const dialogVisible = ref(false)
const dialogTitle = ref('')
const formRef = ref(null)
const formData = reactive({
  name: '',
  key: '',
  type: '',
  description: ''
})
const formRules = {
  name: [{ required: true, message: '请输入流程名称', trigger: 'blur' }],
  key: [{ required: true, message: '请输入流程标识', trigger: 'blur' }],
  type: [{ required: true, message: '请选择流程类型', trigger: 'change' }]
}

// 查询
const handleSearch = () => {
  loading.value = true
  // TODO: 实现查询逻辑
  setTimeout(() => {
    loading.value = false
  }, 500)
}

// 重置
const handleReset = () => {
  searchForm.name = ''
  searchForm.type = ''
  searchForm.status = ''
  handleSearch()
}

// 新增
const handleAdd = () => {
  dialogTitle.value = '新增流程'
  formData.name = ''
  formData.key = ''
  formData.type = ''
  formData.description = ''
  dialogVisible.value = true
}

// 编辑
const handleEdit = (row) => {
  dialogTitle.value = '编辑流程'
  formData.name = row.name
  formData.key = row.key
  formData.type = row.type
  formData.description = row.description
  dialogVisible.value = true
}

// 提交表单
const handleSubmit = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      // TODO: 实现提交逻辑
      ElMessage.success('操作成功')
      dialogVisible.value = false
      handleSearch()
    }
  })
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确认要删除该流程吗？', '提示', {
    type: 'warning'
  }).then(() => {
    // TODO: 实现删除逻辑
    ElMessage.success('删除成功')
    handleSearch()
  })
}

// 设计
const handleDesign = (row) => {
  // TODO: 跳转到流程设计器
  console.log('设计流程', row)
}

// 发布
const handlePublish = (row) => {
  ElMessageBox.confirm('确认要发布该流程吗？', '提示', {
    type: 'warning'
  }).then(() => {
    // TODO: 实现发布逻辑
    ElMessage.success('发布成功')
    handleSearch()
  })
}

// 停用
const handleDisable = (row) => {
  ElMessageBox.confirm('确认要停用该流程吗？', '提示', {
    type: 'warning'
  }).then(() => {
    // TODO: 实现停用逻辑
    ElMessage.success('停用成功')
    handleSearch()
  })
}

// 启用
const handleEnable = (row) => {
  ElMessageBox.confirm('确认要启用该流程吗？', '提示', {
    type: 'warning'
  }).then(() => {
    // TODO: 实现启用逻辑
    ElMessage.success('启用成功')
    handleSearch()
  })
}

// 查看
const handleView = (row) => {
  // TODO: 查看流程详情
  console.log('查看流程', row)
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}

// 获取流程类型标签类型
const getTypeTag = (type) => {
  const map = {
    loan: '',
    risk: 'warning',
    contract: 'success'
  }
  return map[type]
}

// 获取流程类型文本
const getTypeText = (type) => {
  const map = {
    loan: '贷款审批',
    risk: '风险审核',
    contract: '合同审批'
  }
  return map[type]
}

// 获取状态标签类型
const getStatusTag = (status) => {
  const map = {
    published: 'success',
    unpublished: 'info',
    disabled: 'danger'
  }
  return map[status]
}

// 获取状态文本
const getStatusText = (status) => {
  const map = {
    published: '已发布',
    unpublished: '未发布',
    disabled: '已停用'
  }
  return map[status]
}
</script>

<style scoped>
.workflow-definition {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

:deep(.el-card__body) {
  padding: 20px;
}
</style> 